<html>
<body>

<head><title>CS143 Project 1B</title></head>

<p>Type an SQL query in the following box:</p>

<form action="query.php" method="GET">
<textarea name="query" cols="75" rows="4"></textarea>
<input type="submit" value="Submit" />
</form>

<p><small>Note: tables and fields are case sensitive. Run "show tables" to see the list of available tables.</small>

<h3>Results from MySQL:</h3>

<?php 

$db_connection = mysql_connect("localhost", "cs143", "");

mysql_select_db("CS143", $db_connection); ##TEST

$query = $_GET["query"];

if ( $query ) {

  $sanitized_name = mysql_real_escape_string($name, $db_connection);

  $query_to_issue = sprintf($query, $sanitized_name);

  $rs = mysql_query($query_to_issue, $db_connection);

  if (!$db_connection) {
    $errmsg = mysql_error($db_connection);

    print "Connection failed: $errmsg <br />";
    exit(1);
  }

  $numOfCols = mysql_num_fields($rs);
  $numOfRows = mysql_num_rows($rs);

}

if ( $numOfCols > 0 && $numOfRows > 0 ) {

  echo "<table border=2 cellspacing=2 cellpadding=2><tr align=center>";

  for ($i = 0; $i < $numOfCols; $i++) {
    $temp = mysql_field_name($rs, $i);
    echo "<td><b>$temp</b></td>";
  }

  echo "</tr>";

  while($row = mysql_fetch_row($rs)) {
    
    echo "<tr align=center>";  
    for ($j = 0; $j < $numOfCols; $j++) {      
      if ($row[$j] == NULL) {
        echo "<td>N/A</td>";
      } else {       
        echo "<td>$row[$j]</td>";
      }
    }
    echo "</tr>";  
  }
  echo "</table>";
}

mysql_close($db_connection);

?>

<html>
<body>

